<?php

namespace App\Http\Controllers\Client\Activity;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LotteryDetailController extends ClientController
{

    public function index()
    {
        $all = $this->all;
        $act_id = $all['act_id'];

        try {

            //商品详情
            $result = DB::table('lottery_activity as la')
                ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
                ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
                ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
                ->where('la.act_id', '=', $act_id)
                ->select('la.act_id', 'la.act_no', 'la.goods_id', 'la.shop_id', 'la.start_time', 'la.end_time', 'la.status', 'la.is_stop',
                    'lg.goods_name', 'lg.goods_img', 'lg.price', 'lg.detail',
                    'shop.shop_name', 'shop.shop_type', 'shop.contact_man', 'shop.province', 'shop.city', 'shop.district', 'shop.address', 'shop.link_url',
                    'da.short_name')
                ->get()
                ->toArray();
            $result = json_decode(json_encode($result), true);
            $result = $result[0];

            $participate = DB::table('lottery_users_draw as lud')
                ->leftJoin('lottery_draw_detail as ldd', 'lud.participate_id', '=', 'ldd.participate_id')
                ->where('lud.act_id', '=', $act_id)
                ->where('lud.we_id', '=', $this->we_id)
                ->select('lud.participate_id', 'ldd.win_id', 'ldd.published','ldd.consumed','ldd.consumed_at')
                ->get()
                ->toArray();
            $participate = json_decode(json_encode($participate), true);
            $participate = $participate[0] ?? [];

            $result = array_merge($result, $participate);

            // 头图
            $result['goods_img'] = explode(',', $result['goods_img']);
            $result['goods_imgs'] = AliOss::getUrls($result['goods_img']);

            //参与人数
            $result['total'] = DB::table('lottery_users_draw')->where('act_id', '=', $act_id)->count();

            //店铺
            $result['shop_type_name'] = Data::$laifeiShopTypes[$result['shop_type']];

            return $this->setReturnJson(0, 'success', $result);

        } catch (\Exception $e) {
            Log::error(LotteryDetailController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败');
        }


    }

}
